Applied Methods · MA Public Administration
2026-09-25
This session develops the course strand on Data manipulation and descriptive statistics and connects it to group projects.
To work on data, you can import existing data in your environment.
read.csv(), load(), read_excel()To import a file, you need to indicate where it is on your computer.
You can use path and relative path to do that.
Path show the full way on you computer
Relative paths show the way from the current working directory.
You can use getwd() to know on which directory your session is running.
You can change this by specifying a new working directory with setwd
For me the correct way to set the working directory on my session is
Import the data
What is the unit of analysis?
What are the class of the different variables?
How many observation, how many variables?
How was the data collected?
What do the variable codes mean?
How should I analyse the data?
Are variables in the right format?
Are there caracter ecoding that I need to change?
What data processing do I need to do to format the data ?
Inspect the imported data
Before running operation on your data, you need to understand the data and clean it (oftentime).
E.g.
Sometime, you need to manipulate the variables so you can have a better grasp at the data.
EX: We want to study the demographic pressure on municipalities in Geneva.
We thus look at th population over-time by municipality, right?
Trend not so clear for most municipalities.
How did the population change?
Recode to population increase compare to 1950 with the package dplyr
Few commands that are helpful to know with dplyr
%>%mutate()group_by()summarize()ifelse() and case_when()Sometimes, you may want to combine different datasets.
You can use the merge function to do so, but you need identifiers.
You may want to keep only specific observations and/or variables.
You can subset, and select the columns in your data.
Advantage: When large data, takes computational power just to handle and work on the dataset
Disadantage: One reduced, not possible to go back
Advice: filter and sample your data but always keep the original and manipualtion script.
# A tibble: 1,560 × 5
Mun year pop pop1950 pop_ratio_1950
<chr> <dbl> <dbl> <dbl> <dbl>
1 Aire-la-Ville 1950 144 144 1
2 Aire-la-Ville 1951 130 144 0.903
3 Aire-la-Ville 1952 138 144 0.958
4 Aire-la-Ville 1953 138 144 0.958
5 Aire-la-Ville 1954 144 144 1
6 Aire-la-Ville 1955 151 144 1.05
7 Aire-la-Ville 1956 164 144 1.14
8 Aire-la-Ville 1957 166 144 1.15
9 Aire-la-Ville 1958 163 144 1.13
10 Aire-la-Ville 1959 161 144 1.12
# ℹ 1,550 more rows
# A tibble: 3,588 × 5
Mun year pop pop1950 pop_ratio_1950
<chr> <dbl> <dbl> <dbl> <dbl>
1 Aire-la-Ville 1951 130 144 0.903
2 Gy 1963 135 145 0.931
3 Gy 1951 137 145 0.945
4 Aire-la-Ville 1952 138 144 0.958
5 Aire-la-Ville 1953 138 144 0.958
6 Gy 1952 139 145 0.959
7 Gy 1953 139 145 0.959
8 Gy 1954 143 145 0.986
9 Gy 1955 143 145 0.986
10 Aire-la-Ville 1950 144 144 1
# ℹ 3,578 more rows
Datasets can be in different formats
What changes is the unit of analyses.
Important to know what it is, and what I do need for my analyses.
mun year income
1 Mun1 1980 1000
2 Mun1 2000 1300
3 Mun1 2020 1290
4 Mun2 1980 2030
5 Mun2 2000 2050
6 Mun2 2020 2100
Reshape the data_pop object
Type of variables
Univariates statistics
Population VS sample
Metric (numeric)
Ordinal
Categorical
Name Pet
1 Alison Cat
2 André Fish
3 Bastien Dog
4 Jason Dog
5 Elena Dog
6 Oriane Cat
Name Pet Fish Cat Dog
1 Alison Cat 0 1 0
2 André Fish 1 0 0
3 Bastien Dog 0 0 1
4 Jason Dog 0 0 1
5 Elena Dog 0 0 1
6 Oriane Cat 0 1 0
Scope : maximal - minimal value
Median : 50% of observation
Quartile /Percentile
Interquartile space: spread of the middle 50%
Mean : Sum of n observation divided by n
Variance: Average deviation from the mean
Standard deviation: Square root of variance
The summary function give away lots of univariate statistics
[1] 1628.333
[1] 1665
[1] 235736.7
[1] 485.5272
30% 60%
1295 2030
[1] 1000
[1] 2100
Data comes at different levels of analyses.
There can be individual or aggregate data.
Static or dynamic data
Other levels of analyses (e.g. opinion on issue, votes, cantons, even gender or age can be levels of analyses.)
What is the level of analysis of the data_pop?
One level is the municipality level
The second level is the time dimension
Does the municipality have a higher income than the cantonal average?
Does the current year has an income above or under the average ?
When performing data manipulation, it is essential to check the results.
Important to know whether it produced what was expected.
Beforehand: Knowing what you expect.
After the manipulation: Check if the desired outcome has been produced.
Understand what works and what did not to understand where the problem is.
Reshape: How many columns, how many variables
Merge: Observation in data set 1 and 2, number of variables
Recoding: Keep old variable to check if and where are the issue
When you do statistics, you study the whole population, or a sample of it.
This changes your certainty about the produced statistics.
The mean of a population is a true parameter.
The mean of a sample is an estimation of the population mean
Although individual events are random, collectively we observe some patterns
If events are random, they follow a normal distribution.
What is the probability that given what we see, the events are not random?
Never 100% sure of anything.
Some threshold: 95%, 99%, 99.9%
From the bell curv, how many SD do we need to take to be sure at this level?
Cumulative coding challenge
Discuss what types of data would be best suited for your projects
Look for the requested data (OCSTAT, OFS, openswiss.data, ask me and see if we can figure it out!)
Discuss what is the unit of analysis.
Try to download the data and import it in R, any issue?
Try operationalizing one DV and one IV